package com.java.service.impl;

import com.java.domain.Goods;
import com.java.domain.ShopCart;
import com.java.service.ShopCartService;
import com.java.utils.JedisUtil;
import com.java.utils.SerializeUtil;
import com.java.utils.ShopCartUtils;
import redis.clients.jedis.Jedis;
import redis.clients.util.SafeEncoder;

import java.util.ArrayList;
import java.util.List;


/**
 * @version 2020.3.4
 * @User 悠恋
 * @Data 2022/11/25
 * @Time 15:41
 */
public class ShopCartServiceImpl implements ShopCartService {
    private JedisUtil jedisUtil = JedisUtil.getInstance();//实例化工具类
    private Jedis jedis = JedisUtil.getInstance().getJedis();//从中获取Jedis实例
    private JedisUtil.Strings strings = jedisUtil.new Strings();//从中获取Strings字符串类实例

    // 查询当前用户购物车内的所有商品
    @Override
    public List<ShopCart> findAll(String uname) {
        //现在取出
        byte[] trainByte = jedis.get(SafeEncoder.encode("shopcart"));
        //反序列化返回对象
        List<ShopCart> cartList = (List<ShopCart>) SerializeUtil.unserialize(trainByte);
        List<ShopCart> shopCartList = ShopCartUtils.getShopCartList(cartList, uname);
        //System.out.println(shopCartList);
        return shopCartList;
    }

    // 添加商品
    @Override
    public List<ShopCart> addOrUpdate(ShopCart cart) {
        //现在取出
        byte[] trainByte = jedis.get(SafeEncoder.encode("shopcart"));
        //反序列化返回对象
        List<ShopCart> cartList = (List<ShopCart>) SerializeUtil.unserialize(trainByte);
        //添加新加入购物车的商品 如果同一用户重复购买该商品则商品数量加一
        if (cartList == null) {
            cartList = new ArrayList<>();
            cartList.add(cart);
        } else {
            for (ShopCart shopCart : cartList) {
                // 是同一件商品
                if (cart.getGid() == shopCart.getGid() && shopCart.getUname().equals(cart.getUname())) {
                    // 修改商品的数量
                    int num = shopCart.getGnum() + cart.getGnum();
                    shopCart.setGnum(num);
                    // 修改商品的总价
                    shopCart.setGtotal(shopCart.getGprice() * num);
                    // 重新写入redis中
                    strings.set("shopcart", SerializeUtil.serialize(cartList));//序列化好的数据取名为serializeUserinfo存储到redis
                    return cartList;
                }
            }
            //不是同一件商品
            cartList.add(cart);

        }

        // 重新写入redis中
        strings.set("shopcart", SerializeUtil.serialize(cartList));//序列化好的数据取名为serializeUserinfo存储到redis
        //System.out.println(cartList);
        return cartList;

    }

    // 删除单个
    @Override
    public List<ShopCart> delById(int id, String uname) {
        //现在取出
        byte[] trainByte = jedis.get(SafeEncoder.encode("shopcart"));
        //反序列化返回对象
        List<ShopCart> cartList = (List<ShopCart>) SerializeUtil.unserialize(trainByte);
        // 删除redis的数据
        cartList.removeIf(shopCart -> uname.equals(shopCart.getUname()) && id == shopCart.getGid());
        //序列化好的数据取名为serializeUserinfo存储到redis
        strings.set("shopcart", SerializeUtil.serialize(cartList));

        return cartList;

    }

    // 删除所有
    @Override
    public List<ShopCart> delAll(String[] gid, String uname) {
        //现在取出
        byte[] trainByte = jedis.get(SafeEncoder.encode("shopcart"));
        //反序列化返回对象
        List<ShopCart> cartList = (List<ShopCart>) SerializeUtil.unserialize(trainByte);
        Integer[] integerArray = getIntegerArray(gid);
        // 遍历删除
        for (Integer id : integerArray) {
            cartList.removeIf(shopCart -> id == shopCart.getGid() && uname.equals(shopCart.getUname()));
        }
        //序列化好的数据取名为serializeUserinfo存储到redis
        strings.set("shopcart", SerializeUtil.serialize(cartList));
        //System.out.println(cartList);
        List<ShopCart> shopCartList = ShopCartUtils.getShopCartList(cartList, uname);
        //System.out.println(shopCartList);
        return shopCartList;
    }

    // 取出数组里面所有的整形
    public Integer[] getIntegerArray(String[] strings) {
        List<Integer> intArray = new ArrayList<>();
        for (String string : strings) {
            if (!string.equals("true")) {
                intArray.add(Integer.parseInt(string));
            }
        }

        return intArray.toArray(new Integer[0]);

    }
}
